import { OutRequest } from 'src/app/domain/out-request';
import { Component, OnInit } from '@angular/core';
import { OutTest, OutRequestStateService } from 'src/app/service/out-request-state.service';
import { Router, ActivatedRoute } from '@angular/router';
import { ManagerService } from './../../../service/manager.service';
import { User } from 'src/app/domain/user';
import { AuthService } from '../../../service/auth.service';
/*

@Author:赵明洋

@Date: 2018-12-12

@员工外出申请功能实现

@Last Modified time: 2018-12-16
*/
@Component({
  selector: 'app-out-request',
  templateUrl: './out-request.component.html',
  styleUrls: ['./out-request.component.css']
})
export class OutRequestComponent implements OnInit {
  private manager: User;
  sid =AuthService.getUserId();

      // formModule:FormGroup;  
      outTest:OutTest  = { 
        user_id:this.sid,
        start_time:'1',
        end_time:'',
        reason:'',
        state:2,
        duration:0,
      }; 
       outRequest:OutRequest = { 
        id:0,
        user_id:1000,
        start_time:'1',
        end_time:'',
        reason:'',
        state:2,
        duration:0,
        suggestion:"",
      }; 

  constructor(   private router:Router ,  private routeInfo:ActivatedRoute, private outrequestservice :OutRequestStateService ,private managerService: ManagerService) {this.manager = {
    id: null,
    user_name: null,
    name: null,
    age: null,
    sex: null,
    department: null, //部门
    position: null, //职务
    annual_leave_length: null, //年假上限
    remain_annual_leave: null, //剩余年假
  }; }
  id:string;
  start_time:string;
  end_time:string;
  reason:string;
  state:number;
  duration:number;
  type:number;
  morereason:string;

  

  ngOnInit() 
  {
    let outID =this.routeInfo.snapshot.params['id'];
 /*    let fb = new FormBuilder();
    this.formModule = fb.group(
      {
        starttime:['', [Validators.required]],
        endtime:['', [Validators.required]],
       reason:['', [Validators.required]],
       morereason:['', [Validators.required]],     
      }) */
   // this.outRequest=
   if(outID !=0)
   {
    this.outrequestservice.getOutRequest(outID).subscribe(
      data => {
        this.outRequest= data[0];
        //console.log(data[0].reason);
        this.start_time=this.outRequest.start_time;
        this.end_time=this.outRequest.end_time;
        this.reason=this.outRequest.reason;
        this.duration=this.outRequest.duration;
        
    }
    )
  }
    
  
   // this.leaveTest ={ 
   //   user_id:1000,
   //   start_time:"2018/1/1",
   //   end_time:"2018/2/2 ",
   //   reason:"舒服",
   //   state:0,
   //   duration:11,
   //   type:1
   // 
  //  };
   
  }

  cancel() //取消按钮函数
  {
       this.router.navigateByUrl('/staff/outrequeststate');
  }

  save(): void {
  //利用表单数据绑定把我想要的数据全部更新一次
   this.outTest.start_time=this.start_time;
   this.outTest.end_time=this.end_time;
   this.outTest.reason=this.reason;
   this.outTest.duration=this.duration;
   this.outRequest.start_time=this.start_time;
   this.outRequest.end_time=this.end_time;
   this.outRequest.reason=this.reason;
   this.outRequest.duration=this.duration;
   this.outTest.state=2;
   this.outRequest.state=2;
    
    if(this.outRequest.id !=0)
    {
      this.outrequestservice.upDateoutRequest(this.outRequest)
    .subscribe(data => console.log(data));
    this.router.navigateByUrl('/staff/outrequeststate');
    }
    else{
      this.outrequestservice.addOutRequest(this.outTest)
      .subscribe(data => console.log(data));
       this.router.navigateByUrl('/staff/outrequeststate');
    }
  }
}
